package com.edu.robot.modules.robot.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.robot.model.R;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.modules.robot.controller.vo.PageQuery;
import com.edu.robot.modules.robot.controller.vo.PromptVO;
import com.edu.robot.modules.robot.corvert.PromptCovert;
import com.edu.robot.modules.robot.dto.PageResult;
import com.edu.robot.modules.robot.dto.PromptDTO;
import com.edu.robot.modules.robot.mapper.CrPromptMapper;
import com.edu.robot.modules.robot.model.CrPrompt;
import com.edu.robot.modules.robot.service.CrPromptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author 86177
* @description 针对表【cr_prompt】的数据库操作Service实现
* @createDate 2025-02-22 14:34:38
*/
@Service
@Slf4j
public class CrPromptServiceImpl extends ServiceImpl<CrPromptMapper, CrPrompt>
    implements CrPromptService {

	@Override
	public R<Boolean> createPrompt(PromptVO promptVO) {

		log.info("CrPromptServiceImpl createPrompt promptVO:{}", promptVO);

		if (Objects.isNull(promptVO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		CrPrompt convert = PromptCovert.convert(promptVO);

		if (!save(convert)){
			throw new BizException(ResultCodeEnum.DATABASE_ERROR);
		}

		return R.success(true);
	}

	@Override
	public PromptDTO getPromptById(Long id) {

		if (Objects.isNull(id)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		CrPrompt crPrompt = getById(id);

		if (Objects.isNull(crPrompt)){
			throw new BizException(ResultCodeEnum.DATA_NOT_FOUND,"没有查询到对应的提示词数据");
		}

		return PromptCovert.convert(crPrompt);
	}

	@Override
	public R<Boolean> updatePrompt(PromptVO promptVO) {

		if(Objects.isNull(promptVO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		if(Objects.isNull(promptVO.getId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "Invalid promptId Cant not be null");
		}

		CrPrompt convert = PromptCovert.convert(promptVO);

		if(!updateById(convert)){
			throw new BizException(ResultCodeEnum.DATABASE_ERROR);
		}


		return R.success(Boolean.TRUE);
	}

	@Override
	public PageResult<PromptDTO> pagePrompt(PageQuery<PromptVO> query) {

		log.info("CrPromptServiceImpl pagePrompt query:{}", query);

		if(Objects.isNull(query)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID);
		}

		//默认分页参数和分页大小
		Long currentPage = Optional.ofNullable(query.getPageNum()).orElse(1L);
		Long size = Optional.ofNullable(query.getPageSize()).orElse(10L);

		PromptVO param = query.getQuery();

		LambdaQueryWrapper<CrPrompt> queryWrapper = new LambdaQueryWrapper<>();

		if (!Objects.isNull(param)){

			if(StringUtils.isNotBlank(param.getName())){
				queryWrapper.like(CrPrompt::getName, param.getName());
			}

			if(StringUtils.isNotBlank(param.getStatus())){
				queryWrapper.eq(CrPrompt::getStatus, param.getStatus());
			}

			if (StringUtils.isNotBlank(param.getId())){
				queryWrapper.eq(CrPrompt::getId, param.getId());
			}

		}

		queryWrapper.orderByDesc(CrPrompt::getCreatedAt);

		IPage<CrPrompt> pageParam = new Page<>(currentPage, size);

		IPage<CrPrompt> pageResult = page(pageParam, queryWrapper);

		if (Objects.isNull(pageResult)){
			throw new BizException(ResultCodeEnum.DATABASE_ERROR);
		}

		if (pageResult.getRecords().isEmpty()){
			return PageResult.of(0L, 0L, 0L, null);
		}

		PageResult<PromptDTO> res = new PageResult<>();
		res.setTotal(pageResult.getTotal());
		res.setTotalPages(pageResult.getPages());
		res.setCurrentPage(pageResult.getCurrent());

		if (CollectionUtil.isNotEmpty(pageResult.getRecords())){
			List<PromptDTO> collect = pageResult.getRecords().stream().map(PromptCovert::convert).collect(Collectors.toList());
			res.setRecords(collect);
		}

		return res;
	}
}




